package cn.tsyz.local.modules.user.controller;

import cn.tsyz.local.common.result.Result;
import cn.tsyz.local.modules.user.model.User;
import cn.tsyz.local.util.UserContextHolder;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api")
public class ApiController {

    @GetMapping("/users")
    public Result<List<User>> getUsers() {
        log.info("获取用户列表，当前用户: {}", JSON.toJSONString(UserContextHolder.getCurrentUser()));

        // 模拟返回用户列表
        List<User> users = Arrays.asList(
                User.builder().id(1L).username("张三").email("zhangsan@example.com").enabled(true).build(),
                User.builder().id(2L).username("李四").email("lisi@example.com").enabled(true).build()
        );

        return Result.success(users);
    }

    @GetMapping("/users/{id}")
    public Result<User> getUserById(@PathVariable Long id) {
        log.info("获取用户列表，当前用户: {}", JSON.toJSONString(UserContextHolder.getCurrentUser()));

        // 模拟根据ID查找用户
        User user = User.builder()
                .id(id)
                .username("测试用户")
                .email("test@example.com")
                .enabled(true)
                .build();

        return Result.success(user);
    }

    @GetMapping("/profile")
    public Result<String> getProfile() {
        User currentUser = UserContextHolder.getCurrentUser();
        if (currentUser != null) {
            String profile = "欢迎, " + currentUser.getUsername() + "! 您的邮箱是: " + currentUser.getEmail();
            return Result.success(profile);
        }
        return Result.success("未登录");
    }
}
